Create a Base Map

state <- map_data("state")
county <- map_data("county")
#apsu_point <- data.frame("x" = -87.353069, "y" = 36.533654)
ky <- county %>% 
  filter(region=="kentucky")

ggplot() + geom_polygon(data = state, aes(x=long, y = lat, group = group),
                        fill = "white", color="black") + 
           geom_polygon(data = ky, aes(x=long, y = lat, group = group),
                        fill = "gray", color="black") + 
  coord_fixed(xlim = c(-90, -82),  ylim = c(36.5, 39), ratio = 1.2) + 
  xlab("Longitude") + ylab("Latitude") + ggtitle("Kentucky")

yay! now we have the state of Kentucky, lets add some more data

kad_co_streams<- sf::read_sf(("~/Desktop/aculley_KAD_master_folder/KAD_base_maps/KAD_base_maps/kadstreamdata/kad_stream_data.shp"))
kad_watershed<- sf::read_sf(("~/Desktop/aculley_KAD_master_folder/KAD_base_maps/KAD_base_maps/HUC8_CONUS/HUC8_US.shp"))
kad_local <- read.csv("/Users/alexisculley/Desktop/aculley_KAD_master_folder/KAD_base_maps/KAD_base_maps/kad_localities.csv")
sur_mine <-  sf::read_sf(("/Users/alexisculley/Desktop/aculley_KAD_master_folder/KAD_base_maps/KAD_base_maps/Shapefile/5072/2019_activeMining_5072.shp"))

Now we need to graph these to see what they look like. We added a datset including watershed outlines, localities, and streams.

ggplot(kad_co_streams)+geom_sf()

kad_co_rivers <- subset(kad_co_streams, NAME == "South Fork Kentucky River" | NAME == "Middle Fork Kentucky River" | NAME == "North Fork Kentucky River" | NAME == "Kentucky River")

KAD streams looks great! But.. Hold up, we cant just plot this because it will take 7 years since this dataset contains the entire United States. Lets filter the watershed data to only include the areas of Kentucky we care about. You can do this is a lot of ways, but I am just going to subset the dataset by ID’s I know from the dataset.

HUC8_KAD <- subset(kad_watershed, HUC8 == "05100201" | HUC8 == "05100202" | HUC8 == "05100203" | HUC8 == "05100204")
ggplot(HUC8_KAD)+geom_sf()

Now that we have data on the scale we need, we have proof they are actually into R.

Let’s plot them on top of each other. First, we need to set the crs for the datasets so they match.

st_crs(kad_co_streams)
## Coordinate Reference System: NA
kad_co_streams_crs = st_set_crs(kad_co_streams, 4326)
st_crs(kad_co_rivers)
## Coordinate Reference System: NA
kad_co_rivers_crs = st_set_crs(kad_co_rivers, 4326)
st_crs(kad_watershed)
## Coordinate Reference System:
##   User input: WGS 84 
##   wkt:
## GEOGCRS["WGS 84",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["latitude",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["longitude",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4326]]
ggplot() + 
  geom_sf(data = kad_co_streams_crs, color = "blue") +
  geom_sf(data = HUC8_KAD, color = "black") +
  coord_sf()

We get issues.

st_crs(kad_co_streams)
## Coordinate Reference System: NA
kad_co_streams_crs = st_set_crs(kad_co_streams, 4326)
st_crs(kad_co_rivers)
## Coordinate Reference System: NA
kad_co_rivers_crs = st_set_crs(kad_co_rivers, 4326)
ggplot() + 
  geom_sf(data = HUC8_KAD, color = "black") +
  geom_sf(data = kad_co_streams_crs, color = "blue") +
  ggtitle("Kentucky Arrow Darter Historical Range") + 
  coord_sf()

the looks awful. lets clip the stream data by the watershed polygons

kad_watershed_streams = st_intersection(kad_co_streams_crs, st_make_valid(HUC8_KAD))
## although coordinates are longitude/latitude, st_intersection assumes that they are planar
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
kad_rivers_streams = st_intersection(kad_co_rivers_crs, st_make_valid(HUC8_KAD))
## although coordinates are longitude/latitude, st_intersection assumes that they are planar
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
test1 = ggplot() + 
  geom_sf(data = HUC8_KAD, color = "black") +
  geom_sf(data = kad_watershed_streams, color = "blue") +
  ggtitle("Kentucky Arrow Darter Historical Range") + 
  coord_sf()

test1

Wow! Now we have all the streams in the watersheds the Kentucky Arrow Darter are found in. This doesn’t tell us a lot though does it?

test2 = ggplot() + 
  geom_sf(data = HUC8_KAD, color = "#9AB988", fill = "white") +
  geom_sf(data = kad_watershed_streams, color = "#F0AF82") +
  geom_sf(data = kad_rivers_streams, color = "#A17F96") +
  geom_point(data = kad_local, aes(x = long, y = lat), color="#151a58", size = 3, alpha = 0.8) +
  geom_text(data=kad_local,aes(x=long,y=lat,label=name), color="#151a58", vjust=-1.0, size=2.5, fontface="bold") +
  labs(x="Longitude",y="Latitude", title="Kentucky Arrow Darter Localities", fill = "No. of Stations") +
  north(kad_watershed_streams, location = "bottomleft", scale = 0.05, symbol = 12) +
  theme(legend.position = "bottom")+
  coord_sf()

##  scalebar(kad_co_streams_crs, dist = 20, transform = TRUE, dist_unit = "mi", model = "WGS84", location = "bottomleft", st.dist = 0.05, st.size = 2) +
  

test2

state <- map_data("state")
county <- map_data("county")
#apsu_point <- data.frame("x" = -87.353069, "y" = 36.533654)
ky <- county %>% 
  filter(region=="kentucky")

ken_ref_map = ggplot() + 
  geom_polygon(data = state, aes(x=long, y = lat, group = group),
                        fill = "white", color="black") + 
           geom_polygon(data = ky, aes(x=long, y = lat, group = group),
                        fill = "gray", color="black") + 
  geom_sf(data = HUC8_KAD, color = "red") +
  coord_sf(xlim = c(-90, -82),  ylim = c(36.5, 39)) + 
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank())
ken_ref_map

gg_inset_map1 = ggdraw() +
  draw_plot(test2) +
  draw_plot(ken_ref_map, x = 0.05, y = 0.65, width = 0.3, height = 0.3)

gg_inset_map1

leaflet(kad_local) %>% 
  addTiles()%>% 
  addCircleMarkers(data = kad_local,
             lat = ~lat, lng = ~long, popup = ~name,
             col = ~name,
             stroke = FALSE, fillOpacity = 0.3, opacity = 0.3)
leaflet(kad_local) %>% 
  addTiles()%>%  
  addCircleMarkers(data = kad_local,
             lat = ~lat, lng = ~long, label = kad_local$name,
                   weight = 2,
                   color = "grey",
                   fillColor = kad_local$name,
                   fillOpacity = 0.7,
             stroke = FALSE, opacity = 0.3)%>%
  addWMSTiles("https://basemap.nationalmap.gov/arcgis/services/USGSTopo/MapServer/WmsServer", layers = "0") %>%
  addMiniMap(zoomLevelOffset = -4) %>%
  addScaleBar()